package com.huitong.admin.system.service.impl;

import com.huitong.admin.system.domain.dto.AdminInfoDTO;
import com.huitong.admin.system.domain.po.AdminInfoPO;
import com.huitong.admin.system.domain.vo.AdminInfoVO;
import com.huitong.admin.system.mapper.AdminMapper;
import com.huitong.admin.system.service.AdminService;
import com.huitong.api.oss.remote.RemoteOSSService;
import com.huitong.common.core.constant.ValidateConstant;
import com.huitong.common.core.domain.Result;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 教务信息服务层
 *
 * @author fromdrowning
 * @date 2024/3/24 9:40
 */
@RequiredArgsConstructor
@Service
public class AdminServiceImpl implements AdminService {

    /**
     * 教务信息数据访问层
     */
    private final AdminMapper adminMapper;

    /**
     * 远程调用文件上传服务
     */
    @DubboReference
    private final RemoteOSSService remoteOSSService;

    /**
     * 查询教务详细信息
     *
     * @param id 学号
     * @return 教务详细信息
     */
    @Override
    public Result<AdminInfoVO> selectInfo(Long id) {
        return Result.success(adminMapper.selectInfo(id));
    }

    /**
     * 修改教务详细信息
     *
     * @param adminInfoDTO 教务详细信息
     * @throws IOException 读取文件失败抛出此异常
     */
    @Override
    public void updateInfo(AdminInfoDTO adminInfoDTO) throws IOException {
        AdminInfoPO adminInfoPO = new AdminInfoPO();
        BeanUtils.copyProperties(adminInfoDTO, adminInfoPO);

        // 只有修改头像时才远程调用上传头像服务
        MultipartFile avatarFile = adminInfoDTO.getAvatarFile();
        if (avatarFile != null && !avatarFile.isEmpty()) {
            adminInfoPO.setAvatarUrl(remoteOSSService.uploadFile(adminInfoDTO.getId() + ValidateConstant.JPG_SUFFIX, avatarFile.getBytes()));
        }

        adminMapper.updateInfo(adminInfoPO);
    }
}
